package com.premedsidekick;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.Html;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class IntroductionActivity extends Activity {
	LinearLayout layout;
	SharedPreferences preferences;
	Editor editor;
	int check;
	AlertDialog alert;
	Intent intent;
	
	
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		//context = getBaseContext();
		layout = new LinearLayout(this);
		
		
		final ImageView image = new ImageView(this);
		image.setImageResource(R.drawable.introduction);
		image.setAdjustViewBounds(true); // set the ImageView bounds to match the Drawable's dimensions
		image.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, 
				LayoutParams.WRAP_CONTENT));
		
		// Add the ImageView to the layout and set the layout as the content view
		layout.addView(image);
		layout.setGravity(Gravity.CENTER_VERTICAL);
		layout.setGravity(Gravity.CENTER_HORIZONTAL);
		setContentView(layout);
		
		//final Handler handler = new Handler();
		Runnable runnable = new Runnable() {
			@Override
			public void run() {
					try {
						Thread.sleep(2500);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					intent = new Intent(IntroductionActivity.this, HomePageActivity.class);
					startActivity(intent);
					/*
					preferences = getSharedPreferences("user_settings", 0);
					editor = preferences.edit();
					final int state = preferences.getInt("accepted", 0);
					check = state;
					
					final AlertDialog.Builder builder = new AlertDialog.Builder(IntroductionActivity.this);

					LayoutInflater inflater = LayoutInflater.from(IntroductionActivity.this);
					ScrollView view = (ScrollView)inflater.inflate(R.layout.disclaimer, null);
					String message = getResources().getString(R.string.disclaimer);
					CharSequence sequence = Html.fromHtml(message);
					builder.setTitle("Disclaimer");
					TextView textview = (TextView)view.findViewById(R.id.textView);
					textview.setText(sequence);
					//builder.setMessage(sequence);
					builder.setCancelable(true);

					if (state < 2) {
						builder.setPositiveButton("I Agree", new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								final int count = state + 1;
								editor.putInt("accepted", count);
								editor.commit();
								alert.dismiss();
								intent = new Intent(IntroductionActivity.this, HomePageActivity.class);
								startActivity(intent);
							}
						});
						builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int which) {
								System.exit(0);
							}
						});
					}
					if (state >= 2) {
						
						builder.setOnCancelListener(new OnCancelListener() {
							@Override
							public void onCancel(DialogInterface dialog) {
								if (check >= 2) {
									alert.dismiss();
									intent = new Intent(IntroductionActivity.this, HomePageActivity.class);
									startActivity(intent);
								}
								else 
									alert.dismiss();
							}
							
						});
					}
					builder.setView(view);
					builder.setIcon(null);
					alert = builder.create();
					alert.show();
				}
				*/
			}
		};
		
		
		new Thread(runnable).start();

	}
	public void onStop() {
		super.onStop();
	}
	public void onResume() {
		super.onResume();
	}
	public void onPause() {
		super.onPause();
	}
}
